import { userBehavior } from './behavior'
import { postFile, postUserInfo } from '@/api/user'
import { setStorage } from '@/utils/storage'
import { toast } from '@/utils/extendApi'
Page({
  behaviors: [userBehavior],
  // 页面的初始数据
  data: {
    isShowPopup: false // 控制更新用户昵称的弹框显示与否
  },
  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      'userInfo.nickname': this.data.userInfo.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false
    })
  },
  async chooseAvatar(event) {
    // console.log(event)
    const { avatarUrl } = event.detail
    const res = await postFile(avatarUrl, 'file')
    const { data } = res
    this.setData({
      'userInfo.headimgurl': data
    })
  },
  getNickName(event) {
    const { nickname } = event.detail.value
    this.setData({
      'userInfo.nickname': nickname,
      isShowPopup: false
    })
  },
  async saveInfo() {
    const res = await postUserInfo(this.data.userInfo)
    // console.log(res)
    if (res.code === 200) {
      this.setUserInfo(this.data.userInfo)
      setStorage('userInfo', this.data.userInfo)
      toast({ title: '用户信息更新成功' })
    }
  }
})
